home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / telecomm / zmdm.zoo / phone.c < prev    next >
C/C++ Source or Header  |  1991-04-27  |  16KB  |  940 lines

  1. /*
  2.  * Phone dialing Module (from XMDM)
  3.  *
  4.  *
  5.  *    Jwahar Bammi
  6.  *     bang:   {any internet host}!dsrgsun.ces.CWRU.edu!bammi
  7.  *     domain: bammi@dsrgsun.ces.CWRU.edu
  8.  *    GEnie:    J.Bammi
  9.  */
  10.  
  11. #include "config.h"
  12.  
  13. #ifndef STANDALONE
  14. #ifdef PHONES
  15.  
  16. #include "zmdm.h"
  17.  
  18. typedef int WORD;
  19. typedef long LONG;
  20.  
  21.  
  22. #define sendchar(c)    Bconout(1, c);
  23. #define clear_screen()  Bconws("\033H\033J")
  24. #define inv()        EscSeq('p')
  25. #define normal()    EscSeq('q')
  26. #define mvto(r,c)    EscSeq('Y');Bconout(2, r+040);Bconout(2, c+040)
  27. #define ceol()        EscSeq('K')
  28. #define show_cursor()    EscSeq('e')
  29. #define hide_cursor()    EscSeq('f')
  30.  
  31. typedef struct _dir {        /* The Directory type */
  32.     char    *name;        /* Name (11 chars max) */
  33.     char     *number;    /* Phone # (24 chars max) */
  34.     WORD    baud;        /* baud Rate         */
  35.     struct _dir *next;    /* Ptr to next entry */
  36. } *DIR;
  37.  
  38.  
  39.      /* External Variables */
  40. extern WORD speed;        /* Current Baud Rate */
  41. extern WORD Baudrate;
  42. extern BAUDS vbauds[];
  43. WORD dchanged = 0;            /* Has the directory been updated */
  44. extern char *PhoneFile;
  45.  
  46.      /* Globals */
  47. static WORD ndir = 0;            /* # of entries in phone directory   */
  48. static DIR directory = (DIR)NULL;    /* The phone directory           */
  49. static DIR lastdir   = (DIR)NULL;     /* Pointer to last entry         */
  50. static char *dirfile = (char *)NULL;    /* Name of file conatining directory */
  51. static WORD rflag = 0;            /* Read directory as yet??          */
  52. static WORD firstTime = 1;        /* first time around              */
  53.  
  54. #if defined(__STDC__) || defined(__cplusplus)
  55. # define P_(s) s
  56. #else
  57. # define P_(s) ()
  58. #endif
  59.  
  60. static WORD readdir P_((void));
  61. static void freedir P_((DIR dir));
  62. static WORD showdir P_((void));
  63. static void putdir P_((WORD first, WORD last));
  64. static void putstr P_((char *s, WORD l));
  65. static DIR nth P_((WORD n));
  66. static void addir P_((void));
  67. static WORD tobaud P_((char *s));
  68. static void opendir P_((void));
  69. static void delentry P_((void));
  70. static WORD jbaud P_((WORD bd));
  71. static char *preadl P_((void));
  72. void dial P_((void));
  73. void redial P_((void));
  74.  
  75. #undef P_
  76.  
  77. /*
  78.  * Read the Telephone directory
  79.  *  returns -2 on read error
  80.  *        -1 if cancelled
  81.  *        -3 if file not found
  82.  *        -4 if env var PHONE not avail or phone file given as arg not accs.
  83.  *         n # of entries  otherwise
  84.  */
  85. static WORD readdir()
  86. {
  87.     register char *filename;
  88.     register WORD handle;
  89.     register WORD nentries;
  90.     register DIR last;
  91.     register DIR present;
  92.     extern char *preadl();
  93. #ifdef __GNUC__
  94.     extern void *malloc(size_t);
  95. #else
  96.     extern char *malloc();
  97. #endif
  98.     extern char *getenv();
  99.     
  100.     if(firstTime == 1)
  101.     {
  102.         firstTime = 0;
  103.         filename = (char *)NULL;
  104.         if(PhoneFile != (char *)NULL)
  105.         {
  106.             filename = PhoneFile;
  107.             if((handle = Fopen(filename, 0)) < 0)
  108.                 filename = (char *)NULL;
  109.             else
  110.                 Fclose(handle);
  111.         }
  112.         if(filename == (char *)NULL)    
  113.             if((filename = getenv("PHONE")) == (char *)NULL)
  114.                 return -4;
  115.     }
  116.     else
  117.     {
  118.       Bconws("Enter Filename of Phone Directory or <CR> to Cancel: ");
  119.  
  120.       if((filename = preadl()) == (char *)NULL)
  121.         /* Cancelled */
  122.         return -1;
  123.     }
  124.     if((dirfile = malloc(strlen(filename)+1)) == (char *)NULL)
  125.     {
  126.         /* Out of memory */
  127.         Bconws("Out of Memory\r\n");
  128.         return 0;
  129.     }
  130.     strcpy(dirfile,filename);
  131.  
  132.     if((handle = Fopen(filename,0)) < 0)
  133.         /* File does not exist */
  134.         return -3;
  135.     
  136.     /* Read in the file */
  137.     if(Fread(handle, 2L, &ndir) != 2L)
  138.     {
  139.         Bconws("Error Reading ");
  140.         Bconws(filename);
  141.         Bconws("\r\n");
  142.         Fclose(handle);
  143.         return -2;
  144.     }
  145.  
  146.     /* Read in the directory */
  147.     last = (DIR)NULL;
  148.     directory = (DIR)NULL;
  149.     lastdir = (DIR)NULL;
  150.     rflag = 1;
  151.     
  152.     for(nentries = 0; nentries < ndir; nentries++)
  153.     {
  154.         /* Allocate an entry */
  155.         if((present = (DIR)malloc(sizeof(struct _dir))) == (DIR)NULL)
  156.         {
  157.             /* Out of memory */
  158.             Bconws("Out of Memory\r\n");
  159.             Fclose(handle);
  160.             return nentries;
  161.         }
  162.         
  163. #ifdef __GNUC__
  164.         if((present->name = malloc((size_t)12)) == (char *)NULL)
  165. #else
  166.         if((present->name = malloc(12)) == (char *)NULL)
  167. #endif
  168.         {
  169.             /* Out of memory */
  170.             Bconws("Out of Memory\r\n");
  171.             Fclose(handle);
  172.             return nentries;
  173.         }
  174.  
  175. #ifdef __GNUC__
  176.         if((present->number = malloc((size_t)25)) == (char *)NULL)
  177. #else
  178.         if((present->number = malloc(25)) == (char *)NULL)
  179. #endif
  180.         {
  181.             /* Out of memory */
  182.             Bconws("Out of Memory\r\n");
  183.             Fclose(handle);
  184.             return nentries;
  185.         }
  186.  
  187.         present->next = (DIR)NULL;
  188.         
  189.         /* Read in the entry */
  190.         if(Fread(handle,11L,present->name) != 11L)
  191.         {
  192.             Bconws("Error Reading ");
  193.             Bconws(filename);
  194.             Bconws("\r\n");
  195.             Fclose(handle);
  196.             rflag = 0;
  197.             freedir(directory);
  198.             return -2;
  199.  
  200.         }
  201.  
  202.         if(Fread(handle,24L,present->number) != 24L)
  203.         {
  204.             Bconws("Error Reading ");
  205.             Bconws(filename);
  206.             Bconws("\r\n");
  207.             Fclose(handle);
  208.             rflag = 0;
  209.             freedir(directory);
  210.             return -2;
  211.         }
  212.  
  213.         if(Fread(handle,2L,&(present->baud)) != 2L)
  214.         {
  215.             Bconws("Error Reading ");
  216.             Bconws(filename);
  217.             Bconws("\r\n");
  218.             Fclose(handle);
  219.             rflag = 0;
  220.             freedir(directory);
  221.             return -2;
  222.         }
  223.         
  224.  
  225.         present->name[11] = '\0';
  226.         present->number[24] = '\0';
  227.  
  228.         /* Link it on with the directory */
  229.         if(last == (DIR)NULL)
  230.             /* first entry */
  231.             directory = present;
  232.         else
  233.             last->next = present;
  234.         
  235.         last = present;
  236.     }
  237.     lastdir = last;
  238.     
  239.     return nentries;    
  240. }
  241.  
  242. /*
  243.  * Free space allocated to a phone directory
  244.  *
  245.  */
  246. static void freedir(dir)
  247. register DIR dir;
  248. {
  249.     register DIR next;
  250.     register DIR present;
  251.  
  252.     for(present = dir; present != (DIR)NULL; present = next)
  253.     {
  254.         next = present->next;
  255.         free(present->name);
  256.         free(present->number);
  257.         free(present);
  258.     }
  259.     
  260.     if(dirfile != (char *)NULL)
  261.     {
  262.         free(dirfile);
  263.         dirfile   = (char *)NULL;
  264.     }
  265.     
  266.     directory = (DIR)NULL;
  267.     lastdir      = (DIR)NULL;
  268.     ndir      = 0;
  269.     rflag      = 0;
  270. }
  271.  
  272. /*
  273.  * Write out the phone directory
  274.  *  -returns 0 on success 1 otherwise
  275.  *
  276.  */
  277. int writedir()
  278. {
  279.     register DIR dir;
  280.     register WORD fd;
  281.     
  282.     if((rflag == 0) || (dirfile == (char *)NULL) || (dchanged == 0))
  283.         /* Nothing to Save */
  284.         return 0;
  285.  
  286.     /* Create/Open file for write - overwrite if it exists */
  287.     if ((fd = Fcreate(dirfile,0)) < 0) /* Will fail if file is present */
  288.     {
  289.         /* Overwrite existing file */
  290.         if((fd = Fopen(dirfile,1)) < 0)
  291.         {
  292.             Bconws("Cannot Open ");
  293.             Bconws(dirfile);
  294.             Bconws("\r\n");
  295.             return 1;
  296.         }
  297.     }
  298.  
  299.     if(Fwrite(fd,2L,&ndir) != 2L)
  300.     {
  301.         Bconws("Error Writing ");
  302.         Bconws(dirfile);
  303.         Bconws("\r\n");
  304.         Fclose(fd);
  305.         return 1;
  306.     }
  307.     
  308.     for(dir = directory; dir != (DIR)NULL; dir = dir->next)
  309.     {
  310.         if(Fwrite(fd,11L,dir->name) != 11L)
  311.         {
  312.             Bconws("Error Writing ");
  313.             Bconws(dirfile);
  314.             Bconws("\r\n");
  315.             Fclose(fd);
  316.             return 1;
  317.         }
  318.  
  319.         if(Fwrite(fd,24L,dir->number) != 24L)
  320.         {
  321.             Bconws("Error Writing ");
  322.             Bconws(dirfile);
  323.             Bconws("\r\n");
  324.             Fclose(fd);
  325.             return 1;
  326.         }
  327.  
  328.         if(Fwrite(fd,2L,&(dir->baud)) != 2L)
  329.         {
  330.             Bconws("Error Writing ");
  331.             Bconws(dirfile);
  332.             Bconws("\r\n");
  333.             Fclose(fd);
  334.             return 1;
  335.         }
  336.     }
  337.     
  338.     Fclose(fd);
  339.     return 0;
  340. }
  341.  
  342. /*
  343.  * Show the phone directory
  344.  * return the entry # or -1 if cancelled
  345.  *
  346.  */
  347. static WORD showdir()
  348. {
  349.     register WORD first, last;
  350.     register WORD n;
  351.     register char *line;
  352.     extern WORD atoi();
  353.     extern char *preadl();
  354.     
  355.     first = 0;
  356.  
  357.     while(1)
  358.     {
  359.         again:
  360.         clear_screen();
  361.  
  362.         mvto(0,19);
  363.         Bconws("Phone Directory: ");
  364.         Bconws(dirfile);
  365.         Bconws("  ");
  366.         printf("%d",ndir); fflush(stdout);
  367.         Bconws(" Entry(s)");
  368.  
  369.         last = (ndir < (first + 44)) ? ndir : first + 44;
  370.         putdir(first,last);
  371.         
  372.         /* mvto(25,0); */
  373.         Bconws("\r\n");
  374.         inv();
  375.         Bconws("Enter a # or <SPACE><RETURN> for Next Page or <RETURN> to Cancel:");
  376.         normal();
  377.         Bconout(2, ' ');
  378.         if((line = preadl()) == (char *)NULL)
  379.         {
  380.             return -1;
  381.         }
  382.         
  383.         if(isdigit(*line))
  384.         {
  385.             if((n = atoi(line)) >= ndir)
  386.             {
  387.                 Bconws("Invalid Number ");
  388.                 hit_key();
  389.                 goto again;
  390.             }
  391.             
  392.             return n;
  393.         }
  394.  
  395.         if(last == ndir)
  396.             first = 0;
  397.         else
  398.             first += 44;
  399.     }
  400. }
  401.  
  402. /*
  403.  * Put up directory entries on the screen
  404.  *
  405.  */
  406. static void putdir(first,last)
  407. register WORD first;
  408. register WORD last;
  409. {
  410.     register DIR dir;
  411.     register WORD row;
  412.     extern DIR nth();
  413.     
  414.     /* Find the first entry */
  415.     dir = nth(first);
  416.     row = (first % 44) + 1;
  417.  
  418.     hide_cursor();
  419.     inv();
  420.     for(; first < last; first++)
  421.     {
  422.         mvto(row,((first & 1)?41:0));
  423.         if(first < 10)
  424.             Bconout(2, ' ');
  425.         printf("%d",first); fflush(stdout);
  426.         Bconout(2, '|');
  427.         putstr(dir->name,1